<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <!-- 注：如果transition没有添加type=animation的话，mode只有淡入 -->
      <!-- 给组件添加key，与组件关联 作为唯一表示，这样组件在切换的时候才会进行重新的创建与移除 -->
      <xtxBread>
        <xtxBreadItem to="/">首页</xtxBreadItem>
        <transition name="fade-right" mode="out-in">
          <xtxBreadItem :key="topCategory.id">{{ topCategory.name }}</xtxBreadItem>
        </transition>
      </xtxBread>

      <!-- 轮播图 -->
      <XtxCarousel :sliders="sliders" style="height:500px"/>
      <!-- 全部二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in topCategory.children" :key="item.id">
            <a href="javascript:;">
              <img :src="item.picture" alt="">
              <p>{{ item.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 各个分类推荐商品 -->
      <div class="ref-goods" v-for="items in subList" :key="items.id">
        <div class="head">
          <h3>{{ items.name }}</h3>
          <p class="tag">温暖柔软，品质之选</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem v-for="itemsGoods in items.goods" :key="itemsGoods.id" :goodsList="itemsGoods"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { computed, ref, watch } from 'vue'
import { findBanner } from '@/api/home'
import { findTopCategory } from '@/api/category'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import GoodsItem from './component/goods-item.vue'
export default {
  name: 'TopCategory',
  components: {
    GoodsItem
  },
  setup () {
    const sliders = ref([])
    // 获取轮播图数据
    findBanner().then(data => {
      sliders.value = data.result
    })

    // 获取面包屑以及所有对应子分类的数据
    const store = useStore()
    const route = useRoute()
    const topCategory = computed(() => {
      let cate = {}
      // 通过计算属性来获取对应的子分类数据  当前顶级分类的id === 根据路由上的vuex中的category模块中的数据id
      const item = store.state.category.list.find(item => {
        console.log(route.params.id)
        return item.id === route.params.id
      })

      // 判断是否查找到对应的数据 如果有数据的话则赋值到数组中，如果没有的话则直接返回空数组
      if (item) cate = item
      return cate
    })

    // 获取顶级分类子分类数据
    const subList = ref([])
    const getSubList = () => {
      findTopCategory(route.params.id).then(data => {
        subList.value = data.result.children
      })
    }

    // 侦听路由id是否有发生改变，有的话重新加载数据
    watch(() => route.params.id, (newVal) => {
      // // 判断是否有数据
      // newVal && getSubList()

      if (newVal && `/category/${newVal}` === route.path) getSubList()
    }, { immediate: true })

    return { sliders, topCategory, subList }
  }
}
</script>

<style lang="less" scoped>

.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      min-height: 160px;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
}
</style>
